//
//  GoodDetailTopSuckView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/26.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodDetailTopSuckView.h"

@interface GoodDetailTopSuckView()

///商品
@property (nonatomic,strong) UIButton *goodsBtn;

///评价
@property (nonatomic,strong) UIButton *goodsEvaluationBtn;

///详情
@property (nonatomic,strong) UIButton *goodsDetailBtn;

@property (nonatomic,strong) UIView *sliderV;

@property (nonatomic,strong) NSMutableArray *btnArr;

@end

@implementation GoodDetailTopSuckView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.btnArr = [NSMutableArray array];
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    
    for (UIButton *btn in self.btnArr) {
        if (btn.tag == _selectIndex) {
            [btn setTitleColor:MainBlackTextColor forState:normal];
            [UIView animateWithDuration:0.25f animations:^{
                self.sliderV.left = btn.left + 30;
            }];
        }else{
            [btn setTitleColor:MainGrayTextColor forState:normal];
        }
    }
}

#pragma mark - UI
-(void)initUI{
    
    UIView *topLineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    topLineV.backgroundColor = MainBackColor;
    [self addSubview:topLineV];
    UIView *bottomLineV = [[UIView alloc] initWithFrame:CGRectMake(0, 54, ScreenWidth, 1)];
    bottomLineV.backgroundColor = MainBackColor;
    [self addSubview:bottomLineV];
    
    [self addSubview:self.goodsBtn];
    [self addSubview:self.goodsEvaluationBtn];
    [self addSubview:self.goodsDetailBtn];
    [self addSubview:self.sliderV];
    
    [self.btnArr addObject:self.goodsBtn];
    [self.btnArr addObject:self.goodsEvaluationBtn];
    [self.btnArr addObject:self.goodsEvaluationBtn];
}

#pragma mark - lazy
- (UIButton *)goodsBtn{
    if (!_goodsBtn) {
        _goodsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goodsBtn.frame = CGRectMake(30, 15, 80, 30);
        [_goodsBtn setTitle:@"商品" forState:normal];
        [_goodsBtn setTitleColor:MainBlackTextColor forState:normal];
        _goodsBtn.tag = 0;
        [_goodsBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goodsBtn;
}

- (UIButton *)goodsEvaluationBtn{
    if (!_goodsEvaluationBtn) {
        _goodsEvaluationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goodsEvaluationBtn.frame = CGRectMake(ScreenWidth/2-40, 15, 80, 30);
        [_goodsEvaluationBtn setTitle:@"评价" forState:normal];
        [_goodsEvaluationBtn setTitleColor:MainGrayTextColor forState:normal];
        _goodsEvaluationBtn.tag = 1;
        [_goodsEvaluationBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goodsEvaluationBtn;
}
- (UIButton *)goodsDetailBtn{
    if (!_goodsDetailBtn) {
        _goodsDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _goodsDetailBtn.frame = CGRectMake(ScreenWidth-110, 15, 80, 30);
        [_goodsDetailBtn setTitle:@"详情" forState:normal];
        [_goodsDetailBtn setTitleColor:MainGrayTextColor forState:normal];
        _goodsDetailBtn.tag = 2;
        [_goodsDetailBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goodsDetailBtn;
}

- (UIView *)sliderV{
    if (!_sliderV) {
        _sliderV = [[UIView alloc] initWithFrame:CGRectMake(self.goodsBtn.left+25, self.goodsBtn.bottom+5, 20, 4)];
        _sliderV.backgroundColor = RGB(37, 130, 255);
        _sliderV.clipsToBounds = YES;
        _sliderV.layer.cornerRadius = 2;
    }
    return _sliderV;
}


#pragma mark - SEL
-(void)btnClick:(UIButton *)btn{
    for (UIButton *btn1 in self.btnArr) {
        [btn1 setTitleColor:MainGrayTextColor forState:normal];
    }
    
    [btn setTitleColor:MainBlackTextColor forState:normal];
    [UIView animateWithDuration:0.25f animations:^{
        self.sliderV.left = btn.left + 30;
    }];
    
    NSInteger touchIndex = (NSInteger)btn.tag;
    if (self.touchBlock) {
        self.touchBlock(touchIndex);
    }
}


@end
